<template>
  <el-container>
    <el-header class="homeHeader">
      <div class="title">云E办</div>
      <el-dropdown class="userInfo" @command="commandHandler">
        <span class="el-dropdown-link">{{ user }}
          <!-- <i><img src="https://img1.baidu.com/it/u=630334249,276256708&fm=26&fmt=auto"></i> -->
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="userInfo">个人中心</el-dropdown-item>
          <el-dropdown-item command="setting">设置</el-dropdown-item>
          <el-dropdown-item command="logout">注销登录</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-menu router unique-opened>
          <el-submenu
            :index="index+''"
            v-for="(item, index) in this.$router.options.routes"
            :key="index"
            v-if="!item.hidden">
            <template slot="title">
              <i class="el-icon-message" style="color: #409eff;"></i>
              <span>{{ item.name }}</span>
            </template>
            <el-menu-item-group>
              <el-menu-item
                :index="children.path"
                v-for="(children, indexj) in item.children">{{ children.name }}</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <el-breadcrumb v-if="this.$router.currentRoute.path != '/home'">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>{{ this.$router.currentRoute.name }}</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="homeWelcome" v-if="this.$router.currentRoute.path == '/home'">
          欢迎来到云E办！
        </div>
        <router-view class="homeRouterView" />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      user: window.sessionStorage.getItem('user')
    }
  },
  methods: {
    commandHandler (command) {
      if (command == 'logout') {
        // 注销登录
        this.$confirm('此操作将注销登录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 这里调注销登录的接口
          // 清空用户信息
          window.sessionStorage.removeItem('tokenStr')
          window.sessionStorage.removeItem('user')
          // 跳转到登录页
          this.$router.replace('/')
          /* 如果是用Vuex去存储菜单，注销登录的时候同时也要清空菜单
            否则登录进去之后，需要重新刷新才能获取最新的菜单，这是需要注意的地方
          */
          console.log(this.$store.commit('initRoutes', []))
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })       
        })
      } else if (command == 'setting') {
        // 设置
      } else if (command == 'userInfo') {
        // 个人中心
      }
    }
  }
}
</script>

<style>
  .homeHeader {
    background: #409ebb;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 15px;
    box-sizing: border-box;
  }
  .homeHeader>.title {
    font-size: 28px;
    font-family: 华文行楷;
    color: white;
    font-style: oblique;
    letter-spacing: 4px;
  }
  .homeHeader .userInfo {
    cursor: pointer;
  }
  .el-dropdown-link {
    display: inline-block;
    width: 48px;
    height: 48px;
    background: white;
    border-radius: 50%;
    display: flex;
    flex-direction: column;
    text-align: center;
    justify-content: center;
    vertical-align: center;
  }
  .homeWelcome {
    text-align: center;
    font-size: 50px;
    font-size: 华文行楷;
    color: #409ebb;
    padding-top: 50px;

  }
  .homeRouterView {
    margin-top: 8
    px;
  }
</style>